﻿using CRMExam.Database;
using MVCBase.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;

namespace CRMExam.Controllers
{
    [Authorize]
    public class CalendarController : Controller
    {
        CRMExam1Entities db = new CRMExam1Entities();
        public ActionResult Activities()
        {
            return View();
        }

        public JsonResult GetEvents()
        {
            var currentUser = User.Identity.GetUserId();

            List<CalendarViewModel> list = new List<CalendarViewModel>();

            //var currentUser = User.Identity.GetUserId();

            List<AspNetUserMeeting> aspNetUserMeeting = db.AspNetUserMeetings.Where(m => m.AspNetUserId == currentUser).ToList();

            List<Meeting> meetingList = new List<Meeting>();

            foreach (var meeting in aspNetUserMeeting)
            {
                meetingList.Add(meeting.Meeting);
            }



            foreach (var meeting in meetingList)
            {
                CalendarViewModel model = new CalendarViewModel
            {
                id = meeting.Id.ToString(),
                title = "\n" + meeting.Subject,
                start = meeting.StartTime.ToString(),
                end = meeting.EndTime.ToString(),
                allDay = false,
                url = "/Meeting/Details/" + meeting.Id.ToString()
            };

                list.Add(model);
            }
            var rows = list.ToArray();
            return Json(rows, JsonRequestBehavior.AllowGet);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}